package com.itheima.redbaby.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.engine.UserEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.PromptManager;
/**
 * 账户中心界面
 * @author Gaara
 *
 */
public class AccountCenterActivity extends BaseActivity implements OnClickListener {
	private String bonus = "";
	private String level = "";
	private int userId = -1;
	private String userseeion = "MD5";
	private int ordercount = 0;
	private int favoritescount = 0;
	private TextView my_name;
	private TextView my_level;
	private TextView my_bonus;
	private TextView my_order;
	private TextView my_address;
	private TextView my_privilege;
	private TextView my_favorite;
	
	private UserEngine userEngineImpl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_account_activity);
		boolean isLogin = GloableParams.isLogin;
		if(!isLogin){
			PromptManager.showToast(this, "请先登录或者注册");
			Intent intent = new Intent(AccountCenterActivity.this,LoginActivity.class);
			intent.setFlags(ContantValue.FLAGS_ACCOUNTCENTER_ACTIVITY);
			startActivity(intent);
			finish();
		}
		myInit();
	}
	/**
	 * 初始化资源
	 */
	private void myInit() {
		my_name = (TextView) findViewById(R.id.my_name);
		my_level = (TextView) findViewById(R.id.my_level);
		my_bonus = (TextView) findViewById(R.id.my_bonus);
		
		TextView head_back_text = (TextView) findViewById(R.id.head_back_text);
		TextView head_logout = (TextView) findViewById(R.id.head_logout);
		head_back_text.setOnClickListener(this);
		head_logout.setOnClickListener(this);
		
		my_order = (TextView) findViewById(R.id.my_order);
		my_address = (TextView) findViewById(R.id.my_address);
		my_privilege = (TextView) findViewById(R.id.my_privilege);
		my_favorite = (TextView) findViewById(R.id.my_favorite);
		
		LinearLayout my_favorite_lin = (LinearLayout) findViewById(R.id.my_favorite_lin);
		LinearLayout my_privilege_lin = (LinearLayout) findViewById(R.id.my_privilege_lin);
		LinearLayout my_address_lin = (LinearLayout) findViewById(R.id.my_address_lin);
		LinearLayout my_order_lin = (LinearLayout) findViewById(R.id.my_order_lin);
		my_favorite_lin.setOnClickListener(this);
		my_privilege_lin.setOnClickListener(this);
		my_address_lin.setOnClickListener(this);
		my_order_lin.setOnClickListener(this);
		
//		my_order.setOnClickListener(this);
//		my_address.setOnClickListener(this);
//		my_privilege.setOnClickListener(this);
//		my_favorite.setOnClickListener(this);
	}
	/**
	 * 后台加载用户
	 */
	private void prepareData(){
		
		new MyHttpTask() {
			@Override
			protected Object doInBackground(Void... params) {
				//模拟链接延迟.
				SystemClock.sleep(500);
				userEngineImpl = DaoAndEngineFactory.getImpl(UserEngine.class);
				return userEngineImpl.getUserInfo();
			}

			@Override
			protected void onPostExecute(Object result) {
				if(result != null){
					JSONObject json = (JSONObject) result;
					String jsonString = json.toJSONString();
					if(jsonString.contains("error")){
						//获取失败
						PromptManager.showToast(AccountCenterActivity.this, json.getString("error"));
					}else{
//						"bonus": 3002,                 
//						"level": "金卡",                 
//						"userId": 30505,
//						"usersession": "MD5",
//						"ordercount":20,        
//						"favoritescount":12                
						bonus = json.getIntValue("bonus")+"";
						level = json.getString("level");
						userId = json.getIntValue("userId");
						userseeion = json.getString("usersession");
						ordercount = json.getIntValue("ordercount");
						favoritescount = json.getIntValue("favoritescount");
						
						updateUserInfo();
					}
				}else{
					PromptManager.showToast(AccountCenterActivity.this, "服务器连接异常...");
				}
			}
		}.executeHttp();
	}
	@Override
	protected void onResume() {
		prepareData();
		super.onResume();
	}
	/**
	 * 更新用户数据.
	 */
	private void updateUserInfo() {
		my_name.setText(GloableParams.username);
		my_level.setText(level);
		my_bonus.setText(bonus);
		my_order.setText("我的订单("+ordercount+")");
		my_address.setText("地址管理");
		my_privilege.setText("优惠券/礼品卡(3)");
		my_favorite.setText("收藏夹("+favoritescount+")");
	}
	@Override
	public void onClick(View v) {
		int id = v.getId();
		Intent intent;
		switch (id) {
		case R.id.head_back_text://返回
			finish();
			break;
		case R.id.head_logout://注销
			//TODO gaara注销也应该开启子线程(post)访问服务器,获取真实的返回数据.
			if(userEngineImpl != null){
				userEngineImpl.changeUserState(false,-1, "");
			}
			finish();
			break;
		case R.id.my_order_lin://我的订单
			intent = new Intent(AccountCenterActivity.this, OrderActivity.class);
			startActivity(intent);
			break;
		case R.id.my_address_lin://地址管理
			intent = new Intent(AccountCenterActivity.this, AddressManageActivity.class);
			startActivity(intent);
			break;
		case R.id.my_privilege_lin://优惠券
			intent = new Intent(AccountCenterActivity.this, MyTicketActivity.class);
			startActivity(intent);
			break;
		case R.id.my_favorite_lin://收藏夹
			intent = new Intent(AccountCenterActivity.this, FavoritesListActivity.class);
			startActivity(intent);
			break;
		}
	}
}
